﻿Public Class HopDongLD
    Public dau As Boolean = False
    Public manv As String = ""
    Public mahd As String = ""
    Private pb As New List(Of DTO_PhongBan.PhongBan)
    Private dataDSHD As DataTable
    Private dsLoc As New ArrayList
    Private dsnv As ArrayList
    Private dsHDCT As ArrayList
    Private xlnv As XLNhanVien
    Private xlhd As XLHopDong
    Public state As Boolean = False
    Private dataCT As New DataTable
    Dim vt As Integer
    Private Sub them_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles them.Click
        If state = True Then
            Dim frmthem As New ThemHD
            dau = True
            frmthem.Tag = lv_HDNV
            frmthem.ShowDialog()
        Else
            MessageBox.Show("Chưa Lựa Chọn Đối Tượng", "Thông Báo", MessageBoxButtons.OK)
        End If
    End Sub
    Private Sub capnhat_(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles capnhat.Click
        If state = True Then
            Dim frmthem As New ThemHD
            dau = False
            frmthem.mahd = xlhd.BUS_MAHD(manv)
            If frmthem.mahd <> "" Then
                frmthem.ShowDialog()
            End If
        Else
            MessageBox.Show("Chưa Lựa Chọn Đối Tượng", "Thông Báo", MessageBoxButtons.OK)
        End If
    End Sub
    Private Sub xoa_(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xoa.Click
        For Each dr As DataRow In dataDSHD.Rows
            If dr("MaHD") = lv_HDNV.Items(vt).SubItems(1).Text Then
                For Each dr1 As DataRow In dataCT.Rows
                    If dr1("MaHD") = dr("MaHD") Then
                        lv_HDNV.Items.RemoveAt(vt)
                        dr1.Delete()
                        dr.Delete()
                        Exit For
                    End If
                Next
                Exit For
            End If
        Next
    End Sub
    Private Sub reload_(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles reload.Click
        RLoad()
    End Sub
    Private Sub thoat_(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles close_.Click
        Me.Close()
    End Sub
    Private Sub luu_(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save.Click
        xlhd = New XLHopDong
        If xlhd.BUS_SaveHD1(dataDSHD, dataCT) = True Then
            MessageBox.Show("Lưu Thành Công ", "Thông Báo", MessageBoxButtons.OK)
        End If
    End Sub

    Private Sub HopDongLD_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RLoad()
    End Sub
    Private Sub RLoad()
        dataDSHD = New DataTable
        xlhd = New XLHopDong
        xlnv = New XLNhanVien
        dsnv = New ArrayList
        dsHDCT = New ArrayList
        dataDSHD = xlhd.BUS_AllDSHD
        dsHDCT = xlhd.BUS_ArrDSCTHD(dataCT)
        dsnv = xlnv.BUS_ArrDSNV
        showLV()
        dsLoc = xlhd.BUS_ArrDSLC(pb)
        For Each s As String In dsLoc
            loc.Items.Add(s)
        Next
        loc.SelectedIndex = 0
    End Sub

    Private Sub loc_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles loc.SelectedIndexChanged
        If loc.SelectedIndex <> 0 Then
            'tim nv va hd co ten phong
            dataDSHD = New DataTable
            xlnv = New XLNhanVien
            dsnv = New ArrayList
            dataDSHD = New DataTable
            xlhd.BUS_DKDSHD(dsnv, dataDSHD, loc.Text, pb)
            showLV()
        End If
    End Sub
    Private Sub showLV()
        xlhd = New XLHopDong
        Lv_dsnv.Items.Clear()
        Lv_dsnv.View = View.Details
        Lv_dsnv.GridLines = True
        Lv_dsnv.FullRowSelect = True
        For Each n As DTO_NhanVienCT.NhanVien In dsnv
            Dim item As New ListViewItem
            item.Text = "STT"
            item.SubItems.Add(n.MaNV)
            item.SubItems.Add(n.HoTen)
            If xlhd.KtraKyHD(n.MaNV, dsHDCT) = True Then
                item.BackColor = Color.Aquamarine
            Else
                item.BackColor = Color.Yellow
            End If
            Lv_dsnv.Items.Add(item)
            capnhatSTT()
        Next
        lv_HDNV.Items.Clear()
        lv_HDNV.View = View.Details
        lv_HDNV.GridLines = True
        lv_HDNV.FullRowSelect = True
        For Each dr As DataRow In dataDSHD.Rows
            Dim item As New ListViewItem
            item.Text = "STT"
            item.SubItems.Add(dr("MaHD"))
            Dim s As String = xlhd.BUS_LayMaNV(dr("MaHD"))
            For Each n As DTO_NhanVienCT.NhanVien In dsnv
                If n.MaNV = s Then
                    item.SubItems.Add(n.HoTen)
                End If
            Next
            item.SubItems.Add(Convert.ToString(dr("NNghiep")))
            item.SubItems.Add(dr("SoLD"))
            item.SubItems.Add(Convert.ToString(dr("NgCap")))
            item.SubItems.Add(Convert.ToString(dr("NCap")))
            item.SubItems.Add(dr("LoaiHD"))
            item.SubItems.Add(dr("THan"))
            item.SubItems.Add(dr("NgKy"))
            item.SubItems.Add(dr("NgBD"))
            item.SubItems.Add(dr("NgKT"))
            item.SubItems.Add(dr("NgBDTV"))
            item.SubItems.Add(dr("NgKTTV"))
            item.SubItems.Add(Convert.ToString(dr("CDanh")))
            item.SubItems.Add(Convert.ToString(dr("CVu")))
            item.SubItems.Add(Convert.ToString(dr("CViec")))
            item.SubItems.Add(Convert.ToString(dr("DCLam")))
            item.SubItems.Add(Convert.ToString(dr("LuongCB")))
            item.SubItems.Add(Convert.ToString(dr("TrLuong")))
            item.SubItems.Add(Convert.ToString(dr("NgTra")))
            item.SubItems.Add(Convert.ToString(dr("PCap")))
            item.SubItems.Add(Convert.ToString(dr("BHiem")))
            item.SubItems.Add(Convert.ToString(dr("GLam")))
            item.SubItems.Add(Convert.ToString(dr("TrYeu")))
            item.SubItems.Add(Convert.ToString(dr("GChu")))
            item.SubItems.Add(Convert.ToString(dr("TTrang")))
            If xlhd.BUS_KtraKTHD(dr("NgBDTV"), dr("NgKTTV")) = True Then
                item.BackColor = Color.Magenta
            ElseIf xlhd.BUS_KtraKTHD(dr("NgBD"), dr("NgKT")) = True Then
                item.BackColor = Color.Red
            End If
            lv_HDNV.Items.Add(item)
            capnhatSTT()
        Next
    End Sub
    Private Sub capnhatSTT()
        For i As Integer = 1 To Lv_DSNV.Items.Count
            Lv_DSNV.Items(i - 1).Text = i.ToString
        Next
    End Sub

    Private Sub lv_HDNV_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lv_HDNV.SelectedIndexChanged
        Try
            vt = lv_HDNV.SelectedItems(0).Index
            mahd = lv_HDNV.SelectedItems(0).SubItems(1).Text()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub HopDongLD_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Dim dr As DialogResult = MessageBox.Show("Bạn có muốn lưu dữ liệu lại trước khi đóng màn hình này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If dr = Windows.Forms.DialogResult.Yes Then
            xlhd = New XLHopDong
            If xlhd.BUS_SaveHD1(dataDSHD, dataCT) = True Then
                MessageBox.Show("Lưu Thành Công ", "Thông Báo", MessageBoxButtons.OK)
            End If
        End If
    End Sub

    Private Sub Lv_dsnv_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Lv_dsnv.SelectedIndexChanged
        Try
            state = True
            manv = Lv_dsnv.SelectedItems(0).SubItems(1).Text()
        Catch ex As Exception
        End Try
        
    End Sub
End Class
'Dim h As New DTO_HopDong.HopDong
'h.NNghiep = lv_HDNV.SelectedItems(0).SubItems(3).Text()
'h.SoLD = lv_HDNV.SelectedItems(0).SubItems(4).Text()
'h.NgCap = lv_HDNV.SelectedItems(0).SubItems(5).Text()
'h.NCap = lv_HDNV.SelectedItems(0).SubItems(6).Text()
'h.TenHD = lv_HDNV.SelectedItems(0).SubItems(7).Text()
'h.THan = lv_HDNV.SelectedItems(0).SubItems(8).Text()
'h.NgKy = lv_HDNV.SelectedItems(0).SubItems(9).Text()
'h.NgHLuc = lv_HDNV.SelectedItems(0).SubItems(10).Text()
'h.NgKT = lv_HDNV.SelectedItems(0).SubItems(11).Text()
'h.NgBDTV = lv_HDNV.SelectedItems(0).SubItems(12).Text()
'h.NgKTTV = lv_HDNV.SelectedItems(0).SubItems(13).Text()
'h.CDanh = lv_HDNV.SelectedItems(0).SubItems(14).Text()
'h.Cvu = lv_HDNV.SelectedItems(0).SubItems(15).Text()
'h.CViec = lv_HDNV.SelectedItems(0).SubItems(16).Text()
'h.DCLam = lv_HDNV.SelectedItems(0).SubItems(17).Text()
'h.LCB = Convert.ToUInt32(lv_HDNV.SelectedItems(0).SubItems(18).Text())
'h.TrLuong = lv_HDNV.SelectedItems(0).SubItems(19).Text()
'h.NgTra = Convert.ToUInt32(lv_HDNV.SelectedItems(0).SubItems(20).Text())
'h.PCap = lv_HDNV.SelectedItems(0).SubItems(21).Text()
'h.BHiem = lv_HDNV.SelectedItems(0).SubItems(22).Text()
'h.GLam = lv_HDNV.SelectedItems(0).SubItems(23).Text()
'h.TrYeu = lv_HDNV.SelectedItems(0).SubItems(24).Text()
'h.GChu = lv_HDNV.SelectedItems(0).SubItems(25).Text()
'h.TTrang = lv_HDNV.SelectedItems(0).SubItems(26).Text()
'dshd.Add(h)